package com.example.a14110100409.yuejiandemo.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a14110100409.yuejiandemo.R;
import com.example.a14110100409.yuejiandemo.fragment.ClassInfoFragment;
import com.example.a14110100409.yuejiandemo.fragment.ClubInfoFragment;
import com.example.a14110100409.yuejiandemo.fragment.VideoInfoFragment;
import com.example.a14110100409.yuejiandemo.model.UserModel;

import it.sephiroth.android.library.picasso.Picasso;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ClubInfoActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout mClub;
    private LinearLayout mClass;
    private LinearLayout mVideo;
    private Fragment mTab6;
    private Fragment mTab7;
    private Fragment mTab8;
    private ImageView clubinfo_pic,clublist_return,club_collect;
    private TextView clubinfo_name;
    private Context mContext;
    private String club_id;
    private int userid;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_info);
        LinearLayoutManager LL = new LinearLayoutManager(this);
        initView();//初始化
        initEvent();//初始化事件
        showData();
        setSelect(0);//
    }

    private void initEvent()
    {
        mClub.setOnClickListener(this);
        mClass.setOnClickListener(this);
        mVideo.setOnClickListener(this);
        clublist_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        club_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collect();
            }
        });

    }

    private void initView()
    {
        mClub = (LinearLayout) findViewById(R.id.clubinfo_club);
        mClass = (LinearLayout) findViewById(R.id.clubinfo_class);
        mVideo = (LinearLayout) findViewById(R.id.clubinfo_video);
        clubinfo_pic = (ImageView) findViewById(R.id.clubinfo_pic);
        clubinfo_name = (TextView) findViewById(R.id.club_infoname);
        clublist_return = (ImageView) findViewById(R.id.clublist_return);
        club_collect=(ImageView) findViewById(R.id.collect);

    }
    private void showData(){
        userid = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getInt("user_id",0);
        Log.v("------------", String.valueOf(userid));
        Intent intent = getIntent();
        club_id = intent.getStringExtra("club_id");
        Log.v("++++++++++++++++",club_id);
        String club_name = intent.getStringExtra("club_name");
        String club_photo = intent.getStringExtra("club_photo");
        clubinfo_name.setText(club_name);
        String address="http://172.24.10.175/workout/Uploads/"+club_photo;
        Picasso.with(mContext).load(address).into(clubinfo_pic);
        getClubState();
    }


    private void setSelect(int i)
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mTab6 == null)
                {
                    mTab6 = new ClubInfoFragment();
                    transaction.add(R.id.id_content1, mTab6);
                } else
                {
                    transaction.show(mTab6);
                }
                break;
            case 1:
                if (mTab7 == null)
                {
                    mTab7 = new ClassInfoFragment()
                    ;transaction.add(R.id.id_content1, mTab7);
                } else
                {
                    transaction.show(mTab7);

                }
                break;
            case 2:
                if (mTab8 == null)
                {
                    mTab8 = new VideoInfoFragment();
                    transaction.add(R.id.id_content1, mTab8);
                } else
                {
                    transaction.show(mTab8);
                }
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (mTab6 != null)
        {
            transaction.hide(mTab6);
        }
        if (mTab7 != null)
        {
            transaction.hide(mTab7);
        }
        if (mTab8 != null)
        {
            transaction.hide(mTab8);
        }
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.clubinfo_club:
                setSelect(0);
                break;
            case R.id.clubinfo_class:
                setSelect(1);
                break;
            case R.id.clubinfo_video:
                setSelect(2);
                break;

            default:
                break;
        }
    }

    private void collect(){
        UserModel userModel = new UserModel();
        Call<Integer> call = userModel.changeClubState(35,club_id);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if(response.body()!=0){
                    Toast.makeText(ClubInfoActivity.this,"关注成功",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });
    }

    private void getClubState(){
        UserModel userModel = new UserModel();
        Call<Integer> call = userModel.getClubState(club_id,userid);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body()==0){
                    club_collect.setImageResource(R.drawable.tab_collect_presse);
                }else{
                    club_collect.setImageResource(R.drawable.tab_collect_normal);
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });
    }
}
